package com.crape.account.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.crape.account.bo.IUploadBO;
import com.crape.common.constants.ConstantCode;
import com.crape.common.enums.ResultType;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.ImageInfo;
import com.crape.common.utils.FileUtils;

@Controller
public class UploadController {
	
	private IUploadBO uploadBOImpl;
	
	@Autowired
	public void setUploadBOImpl(IUploadBO uploadBOImpl) {
		this.uploadBOImpl = uploadBOImpl;
	}

	/**
	 * 上传文件
	 * @param fileUpload 文件名称
	 */
	@RequestMapping(value="/uploadImages.json" ,method=RequestMethod.POST)
	public ModelAndView uploadImages(HttpServletRequest request, @RequestParam("fileUpload")MultipartFile fileUpload) {
		ModelAndView mv = new ModelAndView();
		try {
			ImageInfo imageInfo = uploadBOImpl.uploadImage(request, fileUpload, ConstantCode.imageSize);
			mv.addObject("flag", ResultType.SUCCESS.getCode());
			mv.addObject("message", "文件上传成功");
            mv.addObject("data", imageInfo);
            return mv;
		} catch (BOException e) {
			mv.addObject("flag", ResultType.FAILURE.getCode());
            mv.addObject("message", e.getMessage());
            return mv;
		}
	}
	
	/**
	 * 上传文件
	 * @param fileUpload 文件名称
	 */
	@RequestMapping(value="/uploadArticleImages.json" ,method=RequestMethod.POST)
	public ModelAndView upload(HttpServletRequest request, @RequestParam("fileUpload")MultipartFile fileUpload) {
		ModelAndView mv = new ModelAndView();
		try {
			ImageInfo imageInfo = uploadBOImpl.uploadImage(request, fileUpload, ConstantCode.imageSize);
			mv.addObject("flag", ResultType.SUCCESS.getCode());
			mv.addObject("message", "文件上传成功");
            mv.addObject("data", imageInfo);
            return mv;
		} catch (BOException e) {
			mv.addObject("flag", ResultType.FAILURE.getCode());
            mv.addObject("message", e.getMessage());
            return mv;
		}
	}

	@RequestMapping("/showUpload.jsp")
    public String showUpload(ModelMap modelMap, HttpServletRequest request) {
        String rootPath = request.getSession().getServletContext().getRealPath("/") + "upload//";
        List<File> directorys = FileUtils.getDirectorys(rootPath);
        modelMap.put("directorys", FileUtils.getFileNames(directorys));
        return "/upload/directorys.html";
    }

    @RequestMapping(value = "/showUpload/{day}", method = RequestMethod.GET)
    public String showUpload(@PathVariable("day") String datetime, ModelMap modelMap, HttpServletRequest request) {
        String rootPath = request.getSession().getServletContext().getRealPath("/") + "upload//";
        String filePath = rootPath + datetime + "//";
        List<File> files = FileUtils.getFiles(filePath);
        modelMap.put("datetime", datetime);
        modelMap.put("files", FileUtils.getFileNames(files));
        return "/upload/files.html";
    }
    
}
